﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LeadExpress.Repository
{
    public class ProductLogRepository
    {
        LeadExpressEntities entities = new LeadExpressEntities();
        
        #region Reference function

        public IQueryable<ProductLog> GetAllByProduct(int id)
        {
            return (from _object in entities.ProductLogs where _object.ProductID == id select _object);
        }

        public ProductLog GetSingle(int id)
        {
            return (from _object in entities.ProductLogs where _object.ID == id select _object).FirstOrDefault();
        }

        public void Create([Bind(Exclude = "ID, DateCreated")] ProductLog log, Product model)
        {
            log.ProductID = model.ID;
            log.Active = model.Active;
            log.Approved = model.Approved;
            log.CategoryID = model.CategoryID;
            log.DistrictID = model.DistrictID;
            log.EndDate = model.EndDate;
            log.ImageURL = model.ImageURL;
            log.IsSpecial = model.IsSpecial;
            log.IsTop = model.IsTop;
            log.LongDesc = model.LongDesc;
            log.MetaDescription = model.MetaDescription;
            log.MetaKeywords = model.MetaKeywords;
            log.ShortDesc = model.ShortDesc;
            log.ShowAllCategories = model.ShowAllCategories;
            log.ShowAllStates = model.ShowAllStates;
            log.StartDate = model.StartDate;
            log.StateID = model.StateID;
            log.SubCategoryID = model.SubCategoryID;
            log.Title = model.Title;
            log.DateCreated = System.DateTime.Now;

            entities.AddToProductLogs(log);
            entities.SaveChanges();
        }

        public void Create([Bind(Exclude = "ID, DateCreated")] ProductLog _object)
        {
            _object.DateCreated = System.DateTime.Now;
            entities.AddToProductLogs(_object);
            entities.SaveChanges();
        }

        public void Delete(int id)
        {
            var _object = (from _list in entities.ProductLogs where _list.ID == id select _list).First();
            entities.DeleteObject(_object);
            entities.SaveChanges();
        }
        
        #endregion
    }
}